agora inbox for pgsql-hackers@postgresql.org
help / color / mirror / Atom feed[PATCH v14 1/2] Key PGSTAT_KIND_RELATION by relfile locator
459+ messages / 2 participants
[nested] [flat]
* [PATCH v14 1/2] Key PGSTAT_KIND_RELATION by relfile locator
@ 2025-10-01 09:45 Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
0 siblings, 0 replies; 459+ messages in thread
From: Bertrand Drouvot @ 2025-10-01 09:45 UTC (permalink / raw)
This patch changes the key used for the PGSTAT_KIND_RELATION statistic kind.
Instead of the relation oid, it now relies on:
- dboid (linked to RelFileLocator's dbOid)
- objoid which is the result of a new macro (namely RelFileLocatorToPgStatObjid())
that computes an objoid based on the RelFileLocator's spcOid and the
RelFileLocator's relNumber.
This is possible as, since b14e9ce7d55c, the objoid is now uint64 and spcOid
and relNumber are 32 bits.
That will allow us to add new stats (add writes counters) and ensure that some
counters (n_dead_tup and friends) are replicated.
The patch introduces pgstat_reloid_to_relfilelocator() to 1) avoid calling
RelationIdGetRelation() to get the relfilelocator based on the relation oid
and 2) handle the partitioned table case.
Please note that:
- when running pg_stat_have_stats('relation',...) we now need to be connected
to the database that hosts the relation. As pg_stat_have_stats() is not
documented publicly, then the changes done in 029_stats_restart.pl look
enough.
- this patch does not handle rewrites so some tests are failing. It's only
intent is to ease the review and should not be pushed without being
merged with the following patch that handles the rewrites.
- it can be used to test that stats are incremented correctly and that we're
able to retrieve them as long as rewrites are not involved.
---
src/backend/postmaster/autovacuum.c | 17 +-
src/backend/utils/activity/pgstat_relation.c | 236 ++++++++++++++++---
src/backend/utils/adt/pgstatfuncs.c | 21 +-
src/include/catalog/pg_tablespace.dat | 4 +
src/include/catalog/pg_tablespace.h | 8 +
src/include/pgstat.h | 15 +-
src/include/utils/pgstat_internal.h | 1 +
src/test/recovery/t/029_stats_restart.pl | 40 ++--
8 files changed, 271 insertions(+), 71 deletions(-)
6.1% src/backend/postmaster/
61.7% src/backend/utils/activity/
4.9% src/backend/utils/adt/
3.2% src/include/catalog/
5.6% src/include/
18.1% src/test/recovery/t/
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 219673db930..4bef7ab7543 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -1992,12 +1992,16 @@ do_autovacuum(void)
bool dovacuum;
bool doanalyze;
bool wraparound;
+ RelFileLocator locator;
if (classForm->relkind != RELKIND_RELATION &&
classForm->relkind != RELKIND_MATVIEW)
continue;
relid = classForm->oid;
+ locator.dbOid = classForm->relisshared ? InvalidOid : MyDatabaseId;
+ locator.spcOid = classForm->reltablespace;
+ locator.relNumber = classForm->relfilenode;
/*
* Check if it is a temp table (presumably, of some other backend's).
@@ -2026,8 +2030,7 @@ do_autovacuum(void)
/* Fetch reloptions and the pgstat entry for this table */
relopts = extract_autovac_opts(tuple, pg_class_desc);
- tabentry = pgstat_fetch_stat_tabentry_ext(classForm->relisshared,
- relid);
+ tabentry = pgstat_fetch_stat_tabentry_by_locator(locator);
/* Check if it needs vacuum or analyze */
relation_needs_vacanalyze(relid, relopts, classForm, tabentry,
@@ -2092,6 +2095,7 @@ do_autovacuum(void)
bool dovacuum;
bool doanalyze;
bool wraparound;
+ RelFileLocator locator;
/*
* We cannot safely process other backends' temp tables, so skip 'em.
@@ -2100,6 +2104,9 @@ do_autovacuum(void)
continue;
relid = classForm->oid;
+ locator.dbOid = classForm->relisshared ? InvalidOid : MyDatabaseId;
+ locator.spcOid = classForm->reltablespace;
+ locator.relNumber = classForm->relfilenode;
/*
* fetch reloptions -- if this toast table does not have them, try the
@@ -2119,8 +2126,7 @@ do_autovacuum(void)
}
/* Fetch the pgstat entry for this table */
- tabentry = pgstat_fetch_stat_tabentry_ext(classForm->relisshared,
- relid);
+ tabentry = pgstat_fetch_stat_tabentry_by_locator(locator);
relation_needs_vacanalyze(relid, relopts, classForm, tabentry,
effective_multixact_freeze_max_age,
@@ -2917,8 +2923,7 @@ recheck_relation_needs_vacanalyze(Oid relid,
PgStat_StatTabEntry *tabentry;
/* fetch the pgstat table entry */
- tabentry = pgstat_fetch_stat_tabentry_ext(classForm->relisshared,
- relid);
+ tabentry = pgstat_fetch_stat_tabentry_ext(relid);
relation_needs_vacanalyze(relid, avopts, classForm, tabentry,
effective_multixact_freeze_max_age,
diff --git a/src/backend/utils/activity/pgstat_relation.c b/src/backend/utils/activity/pgstat_relation.c
index bc8c43b96aa..89bf0cbed56 100644
--- a/src/backend/utils/activity/pgstat_relation.c
+++ b/src/backend/utils/activity/pgstat_relation.c
@@ -17,12 +17,17 @@
#include "postgres.h"
+#include "access/htup_details.h"
#include "access/twophase_rmgr.h"
#include "access/xact.h"
#include "catalog/catalog.h"
+#include "catalog/pg_tablespace.h"
+#include "storage/lmgr.h"
#include "utils/memutils.h"
#include "utils/pgstat_internal.h"
#include "utils/rel.h"
+#include "utils/relmapper.h"
+#include "utils/syscache.h"
#include "utils/timestamp.h"
@@ -36,13 +41,12 @@ typedef struct TwoPhasePgStatRecord
PgStat_Counter inserted_pre_truncdrop;
PgStat_Counter updated_pre_truncdrop;
PgStat_Counter deleted_pre_truncdrop;
- Oid id; /* table's OID */
- bool shared; /* is it a shared catalog? */
+ RelFileLocator locator; /* table's rd_locator */
bool truncdropped; /* was the relation truncated/dropped? */
} TwoPhasePgStatRecord;
-static PgStat_TableStatus *pgstat_prep_relation_pending(Oid rel_id, bool isshared);
+static PgStat_TableStatus *pgstat_prep_relation_pending(RelFileLocator locator);
static void add_tabstat_xact_level(PgStat_TableStatus *pgstat_info, int nest_level);
static void ensure_tabstat_xact_level(PgStat_TableStatus *pgstat_info);
static void save_truncdrop_counters(PgStat_TableXactStatus *trans, bool is_drop);
@@ -60,8 +64,7 @@ pgstat_copy_relation_stats(Relation dst, Relation src)
PgStatShared_Relation *dstshstats;
PgStat_EntryRef *dst_ref;
- srcstats = pgstat_fetch_stat_tabentry_ext(src->rd_rel->relisshared,
- RelationGetRelid(src));
+ srcstats = pgstat_fetch_stat_tabentry_ext(RelationGetRelid(src));
if (!srcstats)
return;
@@ -94,8 +97,10 @@ pgstat_init_relation(Relation rel)
/*
* We only count stats for relations with storage and partitioned tables
+ * and we don't count stats generated during a rewrite.
*/
- if (!RELKIND_HAS_STORAGE(relkind) && relkind != RELKIND_PARTITIONED_TABLE)
+ if ((!RELKIND_HAS_STORAGE(relkind) && relkind != RELKIND_PARTITIONED_TABLE) ||
+ OidIsValid(rel->rd_rel->relrewrite))
{
rel->pgstat_enabled = false;
rel->pgstat_info = NULL;
@@ -130,12 +135,37 @@ pgstat_init_relation(Relation rel)
void
pgstat_assoc_relation(Relation rel)
{
+ RelFileLocator locator;
+
Assert(rel->pgstat_enabled);
Assert(rel->pgstat_info == NULL);
+ /*
+ * Don't associate stats for relations without storage and non partitioned
+ * tables.
+ */
+ if (!RELKIND_HAS_STORAGE(rel->rd_rel->relkind) &&
+ rel->rd_rel->relkind != RELKIND_PARTITIONED_TABLE)
+ return;
+
+ if (rel->rd_rel->relkind != RELKIND_PARTITIONED_TABLE)
+ locator = rel->rd_locator;
+ else
+ {
+ /*
+ * Partitioned tables don't have storage, so construct a synthetic
+ * locator for statistics tracking. Use a reserved pseudo tablespace
+ * OID that cannot conflict with real tablespaces, and the relation
+ * OID as relNumber. This ensures no collision with regular relations
+ * even after OID wraparound.
+ */
+ locator.dbOid = (rel->rd_rel->relisshared ? InvalidOid : MyDatabaseId);
+ locator.spcOid = PSEUDO_PARTITION_TABLE_SPCOID;
+ locator.relNumber = rel->rd_id;
+ }
+
/* Else find or make the PgStat_TableStatus entry, and update link */
- rel->pgstat_info = pgstat_prep_relation_pending(RelationGetRelid(rel),
- rel->rd_rel->relisshared);
+ rel->pgstat_info = pgstat_prep_relation_pending(locator);
/* don't allow link a stats to multiple relcache entries */
Assert(rel->pgstat_info->relation == NULL);
@@ -167,9 +197,13 @@ pgstat_unlink_relation(Relation rel)
void
pgstat_create_relation(Relation rel)
{
+ /* don't track stats for relations without storage */
+ if (!RELKIND_HAS_STORAGE(rel->rd_rel->relkind))
+ return;
+
pgstat_create_transactional(PGSTAT_KIND_RELATION,
- rel->rd_rel->relisshared ? InvalidOid : MyDatabaseId,
- RelationGetRelid(rel));
+ rel->rd_locator.dbOid,
+ RelFileLocatorToPgStatObjid(rel->rd_locator));
}
/*
@@ -181,9 +215,13 @@ pgstat_drop_relation(Relation rel)
int nest_level = GetCurrentTransactionNestLevel();
PgStat_TableStatus *pgstat_info;
+ /* don't track stats for relations without storage */
+ if (!RELKIND_HAS_STORAGE(rel->rd_rel->relkind))
+ return;
+
pgstat_drop_transactional(PGSTAT_KIND_RELATION,
- rel->rd_rel->relisshared ? InvalidOid : MyDatabaseId,
- RelationGetRelid(rel));
+ rel->rd_locator.dbOid,
+ RelFileLocatorToPgStatObjid(rel->rd_locator));
if (!pgstat_should_count_relation(rel))
return;
@@ -213,20 +251,23 @@ pgstat_report_vacuum(Relation rel, PgStat_Counter livetuples,
PgStat_EntryRef *entry_ref;
PgStatShared_Relation *shtabentry;
PgStat_StatTabEntry *tabentry;
- Oid dboid = (rel->rd_rel->relisshared ? InvalidOid : MyDatabaseId);
TimestampTz ts;
PgStat_Counter elapsedtime;
+ RelFileLocator locator;
if (!pgstat_track_counts)
return;
+ locator = rel->rd_locator;
+
/* Store the data in the table's hash table entry. */
ts = GetCurrentTimestamp();
elapsedtime = TimestampDifferenceMilliseconds(starttime, ts);
/* block acquiring lock for the same reason as pgstat_report_autovac() */
- entry_ref = pgstat_get_entry_ref_locked(PGSTAT_KIND_RELATION, dboid,
- RelationGetRelid(rel), false);
+ entry_ref = pgstat_get_entry_ref_locked(PGSTAT_KIND_RELATION, locator.dbOid,
+ RelFileLocatorToPgStatObjid(locator),
+ false);
shtabentry = (PgStatShared_Relation *) entry_ref->shared_stats;
tabentry = &shtabentry->stats;
@@ -285,9 +326,9 @@ pgstat_report_analyze(Relation rel,
PgStat_EntryRef *entry_ref;
PgStatShared_Relation *shtabentry;
PgStat_StatTabEntry *tabentry;
- Oid dboid = (rel->rd_rel->relisshared ? InvalidOid : MyDatabaseId);
TimestampTz ts;
PgStat_Counter elapsedtime;
+ RelFileLocator locator;
if (!pgstat_track_counts)
return;
@@ -325,9 +366,25 @@ pgstat_report_analyze(Relation rel,
ts = GetCurrentTimestamp();
elapsedtime = TimestampDifferenceMilliseconds(starttime, ts);
+ if (rel->rd_rel->relkind != RELKIND_PARTITIONED_TABLE)
+ locator = rel->rd_locator;
+ else
+ {
+ /*
+ * Partitioned tables don't have storage, so construct a synthetic
+ * locator for statistics tracking. Use a reserved pseudo tablespace
+ * OID that cannot conflict with real tablespaces, and the relation
+ * OID as relNumber. This ensures no collision with regular relations
+ * even after OID wraparound.
+ */
+ locator.dbOid = (rel->rd_rel->relisshared ? InvalidOid : MyDatabaseId);
+ locator.spcOid = PSEUDO_PARTITION_TABLE_SPCOID;
+ locator.relNumber = rel->rd_id;
+ }
/* block acquiring lock for the same reason as pgstat_report_autovac() */
- entry_ref = pgstat_get_entry_ref_locked(PGSTAT_KIND_RELATION, dboid,
- RelationGetRelid(rel),
+ entry_ref = pgstat_get_entry_ref_locked(PGSTAT_KIND_RELATION,
+ locator.dbOid,
+ RelFileLocatorToPgStatObjid(locator),
false);
/* can't get dropped while accessed */
Assert(entry_ref != NULL && entry_ref->shared_stats != NULL);
@@ -468,7 +525,16 @@ pgstat_update_heap_dead_tuples(Relation rel, int delta)
PgStat_StatTabEntry *
pgstat_fetch_stat_tabentry(Oid relid)
{
- return pgstat_fetch_stat_tabentry_ext(IsSharedRelation(relid), relid);
+ return pgstat_fetch_stat_tabentry_ext(relid);
+}
+
+PgStat_StatTabEntry *
+pgstat_fetch_stat_tabentry_by_locator(RelFileLocator locator)
+{
+ return (PgStat_StatTabEntry *) pgstat_fetch_entry(
+ PGSTAT_KIND_RELATION,
+ locator.dbOid,
+ RelFileLocatorToPgStatObjid(locator));
}
/*
@@ -476,12 +542,14 @@ pgstat_fetch_stat_tabentry(Oid relid)
* whether the to-be-accessed table is a shared relation or not.
*/
PgStat_StatTabEntry *
-pgstat_fetch_stat_tabentry_ext(bool shared, Oid reloid)
+pgstat_fetch_stat_tabentry_ext(Oid reloid)
{
- Oid dboid = (shared ? InvalidOid : MyDatabaseId);
+ RelFileLocator locator;
- return (PgStat_StatTabEntry *)
- pgstat_fetch_entry(PGSTAT_KIND_RELATION, dboid, reloid);
+ if (!pgstat_reloid_to_relfilelocator(reloid, &locator))
+ return NULL;
+
+ return pgstat_fetch_stat_tabentry_by_locator(locator);
}
/*
@@ -503,14 +571,17 @@ find_tabstat_entry(Oid rel_id)
PgStat_TableXactStatus *trans;
PgStat_TableStatus *tabentry = NULL;
PgStat_TableStatus *tablestatus = NULL;
+ RelFileLocator locator;
+
+ if (!pgstat_reloid_to_relfilelocator(rel_id, &locator))
+ return NULL;
+
+ entry_ref = pgstat_fetch_pending_entry(PGSTAT_KIND_RELATION,
+ locator.dbOid,
+ RelFileLocatorToPgStatObjid(locator));
- entry_ref = pgstat_fetch_pending_entry(PGSTAT_KIND_RELATION, MyDatabaseId, rel_id);
if (!entry_ref)
- {
- entry_ref = pgstat_fetch_pending_entry(PGSTAT_KIND_RELATION, InvalidOid, rel_id);
- if (!entry_ref)
- return tablestatus;
- }
+ return tablestatus;
tabentry = (PgStat_TableStatus *) entry_ref->pending;
tablestatus = palloc_object(PgStat_TableStatus);
@@ -706,8 +777,12 @@ AtPrepare_PgStat_Relations(PgStat_SubXactStatus *xact_state)
record.inserted_pre_truncdrop = trans->inserted_pre_truncdrop;
record.updated_pre_truncdrop = trans->updated_pre_truncdrop;
record.deleted_pre_truncdrop = trans->deleted_pre_truncdrop;
- record.id = tabstat->id;
- record.shared = tabstat->shared;
+
+ if (tabstat->relation != NULL)
+ record.locator = tabstat->relation->rd_locator;
+ else
+ record.locator = tabstat->locator;
+
record.truncdropped = trans->truncdropped;
RegisterTwoPhaseRecord(TWOPHASE_RM_PGSTAT_ID, 0,
@@ -750,7 +825,7 @@ pgstat_twophase_postcommit(FullTransactionId fxid, uint16 info,
PgStat_TableStatus *pgstat_info;
/* Find or create a tabstat entry for the rel */
- pgstat_info = pgstat_prep_relation_pending(rec->id, rec->shared);
+ pgstat_info = pgstat_prep_relation_pending(rec->locator);
/* Same math as in AtEOXact_PgStat, commit case */
pgstat_info->counts.tuples_inserted += rec->tuples_inserted;
@@ -785,8 +860,8 @@ pgstat_twophase_postabort(FullTransactionId fxid, uint16 info,
TwoPhasePgStatRecord *rec = (TwoPhasePgStatRecord *) recdata;
PgStat_TableStatus *pgstat_info;
- /* Find or create a tabstat entry for the rel */
- pgstat_info = pgstat_prep_relation_pending(rec->id, rec->shared);
+ /* Find or create a tabstat entry for the target locator */
+ pgstat_info = pgstat_prep_relation_pending(rec->locator);
/* Same math as in AtEOXact_PgStat, abort case */
if (rec->truncdropped)
@@ -920,17 +995,21 @@ pgstat_relation_reset_timestamp_cb(PgStatShared_Common *header, TimestampTz ts)
* initialized if not exists.
*/
static PgStat_TableStatus *
-pgstat_prep_relation_pending(Oid rel_id, bool isshared)
+pgstat_prep_relation_pending(RelFileLocator locator)
{
PgStat_EntryRef *entry_ref;
PgStat_TableStatus *pending;
+ uint64 objid;
+
+ objid = RelFileLocatorToPgStatObjid(locator);
entry_ref = pgstat_prep_pending_entry(PGSTAT_KIND_RELATION,
- isshared ? InvalidOid : MyDatabaseId,
- rel_id, NULL);
+ locator.dbOid,
+ objid, NULL);
+
pending = entry_ref->pending;
- pending->id = rel_id;
- pending->shared = isshared;
+ pending->id = objid;
+ pending->locator = locator;
return pending;
}
@@ -1009,3 +1088,82 @@ restore_truncdrop_counters(PgStat_TableXactStatus *trans)
trans->tuples_deleted = trans->deleted_pre_truncdrop;
}
}
+
+/*
+ * Convert a relation OID to its corresponding RelFileLocator for statistics
+ * tracking purposes.
+ *
+ * Returns true on success, false if the relation doesn't need statistics
+ * tracking.
+ *
+ * For partitioned tables, constructs a synthetic locator using the relation
+ * OID as relNumber, since they don't have storage.
+ */
+bool
+pgstat_reloid_to_relfilelocator(Oid reloid, RelFileLocator *locator)
+{
+ HeapTuple tuple;
+ Form_pg_class relform;
+ bool result = true;
+
+ /* get the relation's tuple from pg_class */
+ tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(reloid));
+
+ if (!HeapTupleIsValid(tuple))
+ return false;
+
+ relform = (Form_pg_class) GETSTRUCT(tuple);
+
+ /* skip relations without storage and non partitioned tables */
+ if (!RELKIND_HAS_STORAGE(relform->relkind) &&
+ relform->relkind != RELKIND_PARTITIONED_TABLE)
+ {
+ ReleaseSysCache(tuple);
+ return false;
+ }
+
+ if (relform->relkind != RELKIND_PARTITIONED_TABLE)
+ {
+ /* build the RelFileLocator */
+ locator->relNumber = relform->relfilenode;
+ locator->spcOid = relform->reltablespace;
+
+ /* handle default tablespace */
+ if (!OidIsValid(locator->spcOid))
+ locator->spcOid = MyDatabaseTableSpace;
+
+ /* handle dbOid for global vs local relations */
+ if (locator->spcOid == GLOBALTABLESPACE_OID)
+ locator->dbOid = InvalidOid;
+ else
+ locator->dbOid = MyDatabaseId;
+
+ /* handle mapped relations */
+ if (!RelFileNumberIsValid(locator->relNumber))
+ {
+ locator->relNumber = RelationMapOidToFilenumber(reloid,
+ relform->relisshared);
+ if (!RelFileNumberIsValid(locator->relNumber))
+ {
+ ReleaseSysCache(tuple);
+ return false;
+ }
+ }
+ }
+ else
+ {
+ /*
+ * Partitioned tables don't have storage, so construct a synthetic
+ * locator for statistics tracking. Use a reserved pseudo tablespace
+ * OID that cannot conflict with real tablespaces, and the relation
+ * OID as relNumber. This ensures no collision with regular relations
+ * even after OID wraparound.
+ */
+ locator->dbOid = (relform->relisshared ? InvalidOid : MyDatabaseId);
+ locator->spcOid = PSEUDO_PARTITION_TABLE_SPCOID;
+ locator->relNumber = relform->oid;
+ }
+
+ ReleaseSysCache(tuple);
+ return result;
+}
diff --git a/src/backend/utils/adt/pgstatfuncs.c b/src/backend/utils/adt/pgstatfuncs.c
index 9185a8e6b83..db882339f1e 100644
--- a/src/backend/utils/adt/pgstatfuncs.c
+++ b/src/backend/utils/adt/pgstatfuncs.c
@@ -30,6 +30,7 @@
#include "storage/procarray.h"
#include "utils/acl.h"
#include "utils/builtins.h"
+#include "utils/pgstat_internal.h"
#include "utils/timestamp.h"
#include "utils/tuplestore.h"
#include "utils/wait_event.h"
@@ -2004,9 +2005,14 @@ Datum
pg_stat_reset_single_table_counters(PG_FUNCTION_ARGS)
{
Oid taboid = PG_GETARG_OID(0);
- Oid dboid = (IsSharedRelation(taboid) ? InvalidOid : MyDatabaseId);
+ RelFileLocator locator;
- pgstat_reset(PGSTAT_KIND_RELATION, dboid, taboid);
+ /* Get the stats locator from the relation OID */
+ if (!pgstat_reloid_to_relfilelocator(taboid, &locator))
+ PG_RETURN_VOID();
+
+ pgstat_reset(PGSTAT_KIND_RELATION, locator.dbOid,
+ RelFileLocatorToPgStatObjid(locator));
PG_RETURN_VOID();
}
@@ -2363,5 +2369,16 @@ pg_stat_have_stats(PG_FUNCTION_ARGS)
uint64 objid = PG_GETARG_INT64(2);
PgStat_Kind kind = pgstat_get_kind_from_str(stats_type);
+ /* Convert relation OID to relfilenode objid */
+ if (kind == PGSTAT_KIND_RELATION)
+ {
+ RelFileLocator locator;
+
+ if (!pgstat_reloid_to_relfilelocator(objid, &locator))
+ PG_RETURN_BOOL(false);
+
+ objid = RelFileLocatorToPgStatObjid(locator);
+ }
+
PG_RETURN_BOOL(pgstat_have_entry(kind, dboid, objid));
}
diff --git a/src/include/catalog/pg_tablespace.dat b/src/include/catalog/pg_tablespace.dat
index c4cde415219..73ed046be31 100644
--- a/src/include/catalog/pg_tablespace.dat
+++ b/src/include/catalog/pg_tablespace.dat
@@ -10,6 +10,10 @@
#
#----------------------------------------------------------------------
+/*
+ * When adding a new one, ensure it does not conflict with
+ * PSEUDO_PARTITION_TABLE_SPCOID.
+ */
[
{ oid => '1663', oid_symbol => 'DEFAULTTABLESPACE_OID',
diff --git a/src/include/catalog/pg_tablespace.h b/src/include/catalog/pg_tablespace.h
index 3bd4a74f003..e5e818eb0c5 100644
--- a/src/include/catalog/pg_tablespace.h
+++ b/src/include/catalog/pg_tablespace.h
@@ -21,6 +21,14 @@
#include "catalog/genbki.h"
#include "catalog/pg_tablespace_d.h" /* IWYU pragma: export */
+/*
+ * Reserved tablespace OID for partitioned table pseudo locators.
+ * This is not an actual tablespace, just a reserved value to distinguish
+ * partitioned table statistics from regular table statistics. Ensures it does
+ * not conflict with the ones in pg_tablespace.dat.
+ */
+#define PSEUDO_PARTITION_TABLE_SPCOID 1665
+
/* ----------------
* pg_tablespace definition. cpp turns this into
* typedef struct FormData_pg_tablespace
diff --git a/src/include/pgstat.h b/src/include/pgstat.h
index 8e3549c3752..ef740aff26e 100644
--- a/src/include/pgstat.h
+++ b/src/include/pgstat.h
@@ -16,6 +16,7 @@
#include "postmaster/pgarch.h" /* for MAX_XFN_CHARS */
#include "replication/conflict.h"
#include "storage/locktag.h"
+#include "storage/relfilelocator.h"
#include "utils/backend_progress.h" /* for backward compatibility */ /* IWYU pragma: export */
#include "utils/backend_status.h" /* for backward compatibility */ /* IWYU pragma: export */
#include "utils/pgstat_kind.h"
@@ -39,6 +40,12 @@ typedef struct RelationData *Relation;
/* Default directory to store temporary statistics data in */
#define PG_STAT_TMP_DIR "pg_stat_tmp"
+/*
+ * Build a pgstat key Objid based on a RelFileLocator.
+ */
+#define RelFileLocatorToPgStatObjid(locator) \
+ (((uint64) (locator).spcOid << 32) | (locator).relNumber)
+
/* Values for track_functions GUC variable --- order is significant! */
typedef enum TrackFunctionsLevel
{
@@ -179,11 +186,11 @@ typedef struct PgStat_TableCounts
*/
typedef struct PgStat_TableStatus
{
- Oid id; /* table's OID */
- bool shared; /* is it a shared catalog? */
+ uint64 id; /* hash of relfilelocator for stats key */
struct PgStat_TableXactStatus *trans; /* lowest subxact's counts */
PgStat_TableCounts counts; /* event counts to be sent */
Relation relation; /* rel that is using this entry */
+ RelFileLocator locator; /* table's relfilelocator */
} PgStat_TableStatus;
/* ----------
@@ -766,8 +773,8 @@ extern void pgstat_twophase_postabort(FullTransactionId fxid, uint16 info,
void *recdata, uint32 len);
extern PgStat_StatTabEntry *pgstat_fetch_stat_tabentry(Oid relid);
-extern PgStat_StatTabEntry *pgstat_fetch_stat_tabentry_ext(bool shared,
- Oid reloid);
+extern PgStat_StatTabEntry *pgstat_fetch_stat_tabentry_by_locator(RelFileLocator locator);
+extern PgStat_StatTabEntry *pgstat_fetch_stat_tabentry_ext(Oid reloid);
extern PgStat_TableStatus *find_tabstat_entry(Oid rel_id);
diff --git a/src/include/utils/pgstat_internal.h b/src/include/utils/pgstat_internal.h
index 97704421a92..e34b008e893 100644
--- a/src/include/utils/pgstat_internal.h
+++ b/src/include/utils/pgstat_internal.h
@@ -786,6 +786,7 @@ extern void PostPrepare_PgStat_Relations(PgStat_SubXactStatus *xact_state);
extern bool pgstat_relation_flush_cb(PgStat_EntryRef *entry_ref, bool nowait);
extern void pgstat_relation_delete_pending_cb(PgStat_EntryRef *entry_ref);
extern void pgstat_relation_reset_timestamp_cb(PgStatShared_Common *header, TimestampTz ts);
+extern bool pgstat_reloid_to_relfilelocator(Oid reloid, RelFileLocator *locator);
/*
diff --git a/src/test/recovery/t/029_stats_restart.pl b/src/test/recovery/t/029_stats_restart.pl
index cdc427dbc78..4d00087dc6f 100644
--- a/src/test/recovery/t/029_stats_restart.pl
+++ b/src/test/recovery/t/029_stats_restart.pl
@@ -55,10 +55,10 @@ trigger_funcrel_stat();
# verify stats objects exist
$sect = "initial";
-is(have_stats('database', $dboid, 0), 't', "$sect: db stats do exist");
-is(have_stats('function', $dboid, $funcoid),
+is(have_stats($connect_db, 'database', $dboid, 0), 't', "$sect: db stats do exist");
+is(have_stats($db_under_test, 'function', $dboid, $funcoid),
't', "$sect: function stats do exist");
-is(have_stats('relation', $dboid, $tableoid),
+is(have_stats($db_under_test, 'relation', $dboid, $tableoid),
't', "$sect: relation stats do exist");
# regular shutdown
@@ -79,10 +79,10 @@ copy($og_stats, $statsfile) or die "Copy failed: $!";
$node->start;
$sect = "copy";
-is(have_stats('database', $dboid, 0), 't', "$sect: db stats do exist");
-is(have_stats('function', $dboid, $funcoid),
+is(have_stats($connect_db, 'database', $dboid, 0), 't', "$sect: db stats do exist");
+is(have_stats($db_under_test, 'function', $dboid, $funcoid),
't', "$sect: function stats do exist");
-is(have_stats('relation', $dboid, $tableoid),
+is(have_stats($db_under_test, 'relation', $dboid, $tableoid),
't', "$sect: relation stats do exist");
$node->stop('immediate');
@@ -96,10 +96,10 @@ $node->start;
# stats should have been discarded
$sect = "post immediate";
-is(have_stats('database', $dboid, 0), 'f', "$sect: db stats do not exist");
-is(have_stats('function', $dboid, $funcoid),
+is(have_stats($connect_db, 'database', $dboid, 0), 'f', "$sect: db stats do not exist");
+is(have_stats($db_under_test, 'function', $dboid, $funcoid),
'f', "$sect: function stats do exist");
-is(have_stats('relation', $dboid, $tableoid),
+is(have_stats($db_under_test, 'relation', $dboid, $tableoid),
'f', "$sect: relation stats do not exist");
# get rid of backup statsfile
@@ -110,10 +110,10 @@ unlink $statsfile or die "cannot unlink $statsfile $!";
trigger_funcrel_stat();
$sect = "post immediate, new";
-is(have_stats('database', $dboid, 0), 't', "$sect: db stats do exist");
-is(have_stats('function', $dboid, $funcoid),
+is(have_stats($connect_db, 'database', $dboid, 0), 't', "$sect: db stats do exist");
+is(have_stats($db_under_test, 'function', $dboid, $funcoid),
't', "$sect: function stats do exist");
-is(have_stats('relation', $dboid, $tableoid),
+is(have_stats($db_under_test, 'relation', $dboid, $tableoid),
't', "$sect: relation stats do exist");
# regular shutdown
@@ -129,10 +129,10 @@ $node->start;
# no stats present due to invalid stats file
$sect = "invalid_overwrite";
-is(have_stats('database', $dboid, 0), 'f', "$sect: db stats do not exist");
-is(have_stats('function', $dboid, $funcoid),
+is(have_stats($connect_db, 'database', $dboid, 0), 'f', "$sect: db stats do not exist");
+is(have_stats($db_under_test, 'function', $dboid, $funcoid),
'f', "$sect: function stats do not exist");
-is(have_stats('relation', $dboid, $tableoid),
+is(have_stats($db_under_test, 'relation', $dboid, $tableoid),
'f', "$sect: relation stats do not exist");
@@ -145,10 +145,10 @@ append_file($og_stats, "XYZ");
$node->start;
$sect = "invalid_append";
-is(have_stats('database', $dboid, 0), 'f', "$sect: db stats do not exist");
-is(have_stats('function', $dboid, $funcoid),
+is(have_stats($connect_db, 'database', $dboid, 0), 'f', "$sect: db stats do not exist");
+is(have_stats($db_under_test, 'function', $dboid, $funcoid),
'f', "$sect: function stats do not exist");
-is(have_stats('relation', $dboid, $tableoid),
+is(have_stats($db_under_test, 'relation', $dboid, $tableoid),
'f', "$sect: relation stats do not exist");
@@ -307,9 +307,9 @@ sub trigger_funcrel_stat
sub have_stats
{
- my ($kind, $dboid, $objid) = @_;
+ my ($db, $kind, $dboid, $objid) = @_;
- return $node->safe_psql($connect_db,
+ return $node->safe_psql($db,
"SELECT pg_stat_have_stats('$kind', $dboid, $objid)");
}
--
2.34.1
--96iU9DKeu+W+YlSa
Content-Type: text/x-diff; charset=us-ascii
Content-Disposition: attachment;
filename="v14-0002-handle-relation-statistics-correctly-during-rewr.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
* [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState
@ 2026-03-23 20:27 Álvaro Herrera <alvherre@kurilemu.de>
0 siblings, 0 replies; 459+ messages in thread
From: Álvaro Herrera @ 2026-03-23 20:27 UTC (permalink / raw)
This makes the data structure simpler, as we only need a single struct
which can be passed down to all functions.
Also, the inititalization and release sequences are a bit simpler
and there's a single location in charge.
---
src/backend/commands/cluster.c | 307 ++++++++++++++-----------------
src/tools/pgindent/typedefs.list | 2 +-
2 files changed, 135 insertions(+), 174 deletions(-)
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b4acaeb6b93..b86e600af41 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -113,15 +113,6 @@ typedef struct
static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
-/*
- * Everything we need to call ExecInsertIndexTuples().
- */
-typedef struct IndexInsertState
-{
- ResultRelInfo *rri;
- EState *estate;
-} IndexInsertState;
-
/* The WAL segment being decoded. */
static XLogSegNo repack_current_segment = 0;
@@ -134,31 +125,27 @@ static XLogSegNo repack_current_segment = 0;
/*
* Information needed to apply concurrent data changes.
*/
-typedef struct ChangeDest
+typedef struct ChangeContext
{
/* The relation the changes are applied to. */
- Relation rel;
-
- /*
- * The following is needed to find the existing tuple if the change is
- * UPDATE or DELETE. 'ident_key' should have all the fields except for
- * 'sk_argument' initialized.
- */
- Relation ident_index;
- ScanKey ident_key;
- int ident_key_nentries;
+ Relation cc_rel;
/* Needed to update indexes of rel_dst. */
- IndexInsertState *iistate;
+ ResultRelInfo *cc_rri;
+ EState *cc_estate;
/*
- * Sequential number of the file containing the changes.
- *
- * TODO This field makes the structure name less descriptive. Should we
- * rename it, e.g. to ChangeApplyInfo?
+ * Existing tuples to UPDATE and DELETE are located via this index. We
+ * keep the scankey in partially initialized state to avoid repeated work.
+ * sk_argument is completed on the fly.
*/
- int file_seq;
-} ChangeDest;
+ Relation cc_ident_index;
+ ScanKey cc_ident_key;
+ int cc_ident_key_nentries;
+
+ /* Sequential number of the file containing the changes. */
+ int cc_file_seq;
+} ChangeContext;
/*
* Layout of shared memory used for communication between backend and the
@@ -278,29 +265,27 @@ static bool repack_is_permitted_for_relation(RepackCommand cmd,
static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid);
static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
DecodingWorkerShared *shared);
-static void apply_concurrent_changes(BufFile *file, ChangeDest *dest);
+static void apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt);
static void apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate);
+ ChangeContext *chgcxt);
static void apply_concurrent_delete(Relation rel, TupleTableSlot *slot);
static void restore_tuple(BufFile *file, Relation relation,
TupleTableSlot *slot);
static void adjust_toast_pointers(Relation relation, TupleTableSlot *dest,
- TupleTableSlot *src);
-static bool find_target_tuple(Relation rel, ChangeDest *dest,
+ TupleTableSlot *src);
+static bool find_target_tuple(Relation rel, ChangeContext *chgcxt,
TupleTableSlot *locator,
TupleTableSlot *received);
static void process_concurrent_changes(XLogRecPtr end_of_wal,
- ChangeDest *dest,
+ ChangeContext *chgcxt,
bool done);
-static IndexInsertState *get_index_insert_state(Relation relation,
- Oid ident_index_id,
- Relation *ident_index_p);
-static ScanKey build_identity_key(Oid ident_idx_oid, Relation rel_src,
- int *nentries);
-static void free_index_insert_state(IndexInsertState *iistate);
+static void initialize_change_context(ChangeContext *chgcxt,
+ Relation relation,
+ Oid ident_index_id);
+static void release_change_context(ChangeContext *chgcxt);
static void cleanup_logical_decoding(LogicalDecodingContext *ctx);
static void rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Oid identIdx,
@@ -2871,13 +2856,13 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* Apply changes stored in 'file'.
*/
static void
-apply_concurrent_changes(BufFile *file, ChangeDest *dest)
+apply_concurrent_changes(BufFile *file, ChangeContext *chgcxt)
{
- ConcurrentChangeKind kind = '\0';
- Relation rel = dest->rel;
- TupleTableSlot *spilled_tuple;
- TupleTableSlot *old_update_tuple;
- TupleTableSlot *ondisk_tuple;
+ ConcurrentChangeKind kind = '\0';
+ Relation rel = chgcxt->cc_rel;
+ TupleTableSlot *spilled_tuple;
+ TupleTableSlot *old_update_tuple;
+ TupleTableSlot *ondisk_tuple;
MemoryContext apply_cxt;
bool have_old_tuple = false;
@@ -2924,12 +2909,12 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
/*
* Just before an UPDATE or DELETE, we must update the command
- * counter, because the change could refer to a tuple that we
- * have just inserted; and before an INSERT, we have to do this
- * also if the previous command was either update or delete.
+ * counter, because the change could refer to a tuple that we have
+ * just inserted; and before an INSERT, we have to do this also if the
+ * previous command was either update or delete.
*
- * With this approach we don't spend so many CCIs for long
- * strings of only INSERTs, which can't affect one another.
+ * With this approach we don't spend so many CCIs for long strings of
+ * only INSERTs, which can't affect one another.
*/
if (kind == CHANGE_UPDATE_NEW || kind == CHANGE_DELETE ||
(kind == CHANGE_INSERT && (prevkind == CHANGE_UPDATE_NEW ||
@@ -2946,22 +2931,22 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
if (kind == CHANGE_INSERT)
{
- apply_concurrent_insert(rel, spilled_tuple, dest->iistate);
+ apply_concurrent_insert(rel, spilled_tuple, chgcxt);
}
else if (kind == CHANGE_DELETE)
{
- bool found;
+ bool found;
/* Find the tuple to be deleted */
- found = find_target_tuple(rel, dest, spilled_tuple, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, spilled_tuple, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
apply_concurrent_delete(rel, ondisk_tuple);
}
else if (kind == CHANGE_UPDATE_NEW)
{
- TupleTableSlot *key;
- bool found;
+ TupleTableSlot *key;
+ bool found;
if (have_old_tuple)
key = old_update_tuple;
@@ -2969,20 +2954,20 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
key = spilled_tuple;
/* Find the tuple to be updated or deleted. */
- found = find_target_tuple(rel, dest, key, ondisk_tuple);
+ found = find_target_tuple(rel, chgcxt, key, ondisk_tuple);
if (!found)
elog(ERROR, "failed to find target tuple");
/*
* If 'tup' contains TOAST pointers, they point to the old
- * relation's toast. Copy the corresponding TOAST pointers for
- * the new relation from the existing tuple. (The fact that we
- * received a TOAST pointer here implies that the attribute
- * hasn't changed.)
+ * relation's toast. Copy the corresponding TOAST pointers for the
+ * new relation from the existing tuple. (The fact that we
+ * received a TOAST pointer here implies that the attribute hasn't
+ * changed.)
*/
adjust_toast_pointers(rel, spilled_tuple, ondisk_tuple);
- apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, dest->iistate);
+ apply_concurrent_update(rel, spilled_tuple, ondisk_tuple, chgcxt);
ExecClearTuple(old_update_tuple);
have_old_tuple = false;
@@ -3006,7 +2991,7 @@ apply_concurrent_changes(BufFile *file, ChangeDest *dest)
*/
static void
apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
List *recheck;
@@ -3019,8 +3004,8 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
* action that already happened, we have no use for the recheck list of
* indexes returned, so just free it. XXX or maybe just leave it?
*/
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
0,
slot,
NIL, NULL);
@@ -3028,7 +3013,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, 1);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
/*
@@ -3038,7 +3023,7 @@ apply_concurrent_insert(Relation rel, TupleTableSlot *slot,
static void
apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
TupleTableSlot *ondisk_tuple,
- IndexInsertState *iistate)
+ ChangeContext *chgcxt)
{
LockTupleMode lockmode;
TM_FailureData tmfd;
@@ -3065,13 +3050,13 @@ apply_concurrent_update(Relation rel, TupleTableSlot *spilled_tuple,
if (update_indexes == TU_Summarizing)
flags |= EIIT_ONLY_SUMMARIZING;
- recheck = ExecInsertIndexTuples(iistate->rri,
- iistate->estate,
+ recheck = ExecInsertIndexTuples(chgcxt->cc_rri,
+ chgcxt->cc_estate,
flags,
spilled_tuple,
NIL, NULL);
list_free(recheck);
- ResetPerTupleExprContext(iistate->estate);
+ ResetPerTupleExprContext(chgcxt->cc_estate);
}
pgstat_progress_incr_param(PROGRESS_REPACK_HEAP_TUPLES_UPDATED, 1);
@@ -3135,19 +3120,19 @@ restore_tuple(BufFile *file, Relation relation, TupleTableSlot *slot)
ExecForceStoreHeapTuple(tup, slot, false);
/*
- * Next, read any attributes we stored separately into the tts_values array
- * elements expecting them, if any. This matches store_change.
+ * Next, read any attributes we stored separately into the tts_values
+ * array elements expecting them, if any. This matches store_change.
*/
BufFileReadExact(file, &natt_ext, sizeof(natt_ext));
if (natt_ext > 0)
{
- TupleDesc desc = slot->tts_tupleDescriptor;
+ TupleDesc desc = slot->tts_tupleDescriptor;
for (int i = 0; i < desc->natts; i++)
{
CompactAttribute *attr = TupleDescCompactAttr(desc, i);
- varlena *varlen;
- alignas(uint32) varlena varhdr;
+ varlena *varlen;
+ alignas(uint32) varlena varhdr;
void *value;
Size varlensz;
@@ -3219,10 +3204,10 @@ adjust_toast_pointers(Relation relation, TupleTableSlot *dest, TupleTableSlot *s
* not found, return false.
*/
static bool
-find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
+find_target_tuple(Relation rel, ChangeContext *chgcxt, TupleTableSlot *locator,
TupleTableSlot *retrieved)
{
- Form_pg_index idx = dest->ident_index->rd_index;
+ Form_pg_index idx = chgcxt->cc_ident_index->rd_index;
IndexScanDesc scan;
bool retval;
@@ -3233,9 +3218,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
*
* Use the incoming tuple to finalize the scan key.
*/
- for (int i = 0; i < dest->ident_key_nentries; i++)
+ for (int i = 0; i < chgcxt->cc_ident_key_nentries; i++)
{
- ScanKey entry = &dest->ident_key[i];
+ ScanKey entry = &chgcxt->cc_ident_key[i];
AttrNumber attno = idx->indkey.values[i];
entry->sk_argument = locator->tts_values[attno - 1];
@@ -3243,9 +3228,9 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
}
/* XXX no instrumentation for now */
- scan = index_beginscan(rel, dest->ident_index, GetActiveSnapshot(),
- NULL, dest->ident_key_nentries, 0);
- index_rescan(scan, dest->ident_key, dest->ident_key_nentries, NULL, 0);
+ scan = index_beginscan(rel, chgcxt->cc_ident_index, GetActiveSnapshot(),
+ NULL, chgcxt->cc_ident_key_nentries, 0);
+ index_rescan(scan, chgcxt->cc_ident_key, chgcxt->cc_ident_key_nentries, NULL, 0);
retval = index_getnext_slot(scan, ForwardScanDirection, retrieved);
index_endscan(scan);
@@ -3260,7 +3245,7 @@ find_target_tuple(Relation rel, ChangeDest *dest, TupleTableSlot *locator,
* are far too similar to each other.
*/
static void
-process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
+process_concurrent_changes(XLogRecPtr end_of_wal, ChangeContext *chgcxt, bool done)
{
DecodingWorkerShared *shared;
char fname[MAXPGPATH];
@@ -3293,7 +3278,7 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
/*
* Has the worker exported the file we are waiting for?
*/
- if (last_exported == dest->file_seq)
+ if (last_exported == chgcxt->cc_file_seq)
break;
ConditionVariableSleep(&shared->cv, WAIT_EVENT_REPACK_WORKER_EXPORT);
@@ -3301,117 +3286,99 @@ process_concurrent_changes(XLogRecPtr end_of_wal, ChangeDest *dest, bool done)
ConditionVariableCancelSleep();
/* Open the file. */
- DecodingWorkerFileName(fname, shared->relid, dest->file_seq);
+ DecodingWorkerFileName(fname, shared->relid, chgcxt->cc_file_seq);
file = BufFileOpenFileSet(&shared->sfs.fs, fname, O_RDONLY, false);
- apply_concurrent_changes(file, dest);
+ apply_concurrent_changes(file, chgcxt);
BufFileClose(file);
/* Get ready for the next file. */
- dest->file_seq++;
+ chgcxt->cc_file_seq++;
}
/*
- * Initialize IndexInsertState for index specified by ident_index_id.
- *
- * While doing that, also return the identity index in *ident_index_p.
+ * Initialize the ChangeContext struct for the given relation, with
+ * the given index as identity index.
*/
-static IndexInsertState *
-get_index_insert_state(Relation relation, Oid ident_index_id,
- Relation *ident_index_p)
+static void
+initialize_change_context(ChangeContext *chgcxt,
+ Relation relation, Oid ident_index_id)
{
- EState *estate;
- IndexInsertState *result;
- Relation ident_index = NULL;
+ chgcxt->cc_rel = relation;
- result = (IndexInsertState *) palloc0(sizeof(IndexInsertState));
- estate = CreateExecutorState();
+ /* Only initialize fields needed by ExecInsertIndexTuples(). */
+ chgcxt->cc_estate = CreateExecutorState();
- result->rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
- InitResultRelInfo(result->rri, relation, 0, 0, 0);
- ExecOpenIndices(result->rri, false);
+ chgcxt->cc_rri = (ResultRelInfo *) palloc(sizeof(ResultRelInfo));
+ InitResultRelInfo(chgcxt->cc_rri, relation, 0, 0, 0);
+ ExecOpenIndices(chgcxt->cc_rri, false);
/*
- * Find the relcache entry of the identity index so that we spend no extra
- * effort to open / close it.
+ * The table's relcache entry already has the relcache entry for the
+ * identity index; find that.
*/
- for (int i = 0; i < result->rri->ri_NumIndices; i++)
+ chgcxt->cc_ident_index = NULL;
+ for (int i = 0; i < chgcxt->cc_rri->ri_NumIndices; i++)
{
Relation ind_rel;
- ind_rel = result->rri->ri_IndexRelationDescs[i];
+ ind_rel = chgcxt->cc_rri->ri_IndexRelationDescs[i];
if (ind_rel->rd_id == ident_index_id)
- ident_index = ind_rel;
+ {
+ chgcxt->cc_ident_index = ind_rel;
+ break;
+ }
}
- if (ident_index == NULL)
+ if (chgcxt->cc_ident_index == NULL)
elog(ERROR, "failed to find identity index");
- /* Only initialize fields needed by ExecInsertIndexTuples(). */
- result->estate = estate;
-
- *ident_index_p = ident_index;
- return result;
-}
-
-/*
- * Build scan key to process logical changes.
- */
-static ScanKey
-build_identity_key(Oid ident_idx_oid, Relation rel_src, int *nentries)
-{
- Relation ident_idx_rel;
- Form_pg_index ident_idx;
- int n,
- i;
- ScanKey result;
-
- Assert(OidIsValid(ident_idx_oid));
- ident_idx_rel = index_open(ident_idx_oid, AccessShareLock);
- ident_idx = ident_idx_rel->rd_index;
- n = ident_idx->indnkeyatts;
- result = (ScanKey) palloc(sizeof(ScanKeyData) * n);
- for (i = 0; i < n; i++)
+ /* Set up for scanning said identity index */
{
- ScanKey entry;
- Oid opfamily,
- opcintype,
- opno,
- opcode;
+ Form_pg_index indexForm;
- entry = &result[i];
+ indexForm = chgcxt->cc_ident_index->rd_index;
+ chgcxt->cc_ident_key_nentries = indexForm->indnkeyatts;
+ chgcxt->cc_ident_key = (ScanKey) palloc_array(ScanKeyData, indexForm->indnkeyatts);
+ for (int i = 0; i < indexForm->indnkeyatts; i++)
+ {
+ ScanKey entry;
+ Oid opfamily,
+ opcintype,
+ opno,
+ opcode;
- opfamily = ident_idx_rel->rd_opfamily[i];
- opcintype = ident_idx_rel->rd_opcintype[i];
- opno = get_opfamily_member(opfamily, opcintype, opcintype,
- BTEqualStrategyNumber);
+ entry = &chgcxt->cc_ident_key[i];
- if (!OidIsValid(opno))
- elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opfamily = chgcxt->cc_ident_index->rd_opfamily[i];
+ opcintype = chgcxt->cc_ident_index->rd_opcintype[i];
+ opno = get_opfamily_member(opfamily, opcintype, opcintype,
+ BTEqualStrategyNumber);
+ if (!OidIsValid(opno))
+ elog(ERROR, "failed to find = operator for type %u", opcintype);
+ opcode = get_opcode(opno);
+ if (!OidIsValid(opcode))
+ elog(ERROR, "failed to find = operator for operator %u", opno);
- opcode = get_opcode(opno);
- if (!OidIsValid(opcode))
- elog(ERROR, "failed to find = operator for operator %u", opno);
-
- /* Initialize everything but argument. */
- ScanKeyInit(entry,
- i + 1,
- BTEqualStrategyNumber, opcode,
- (Datum) NULL);
- entry->sk_collation = ident_idx_rel->rd_indcollation[i];
+ /* Initialize everything but argument. */
+ ScanKeyInit(entry,
+ i + 1,
+ BTEqualStrategyNumber, opcode,
+ (Datum) NULL);
+ entry->sk_collation = chgcxt->cc_ident_index->rd_indcollation[i];
+ }
}
- index_close(ident_idx_rel, AccessShareLock);
- *nentries = n;
- return result;
+ chgcxt->cc_file_seq = WORKER_FILE_SNAPSHOT + 1;
}
static void
-free_index_insert_state(IndexInsertState *iistate)
+release_change_context(ChangeContext *chgcxt)
{
- ExecCloseIndices(iistate->rri);
- FreeExecutorState(iistate->estate);
- pfree(iistate->rri);
- pfree(iistate);
+ ExecCloseIndices(chgcxt->cc_rri);
+ FreeExecutorState(chgcxt->cc_estate);
+ /* XXX are these pfrees necessary? */
+ pfree(chgcxt->cc_rri);
+ pfree(chgcxt->cc_ident_key);
}
static void
@@ -3450,7 +3417,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
Relation *ind_refs,
*ind_refs_p;
int nind;
- ChangeDest chgdst;
+ ChangeContext chgcxt;
/* Like in cluster_rel(). */
lockmode_old = ShareUpdateExclusiveLock;
@@ -3496,12 +3463,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
errmsg("identity index missing on the new relation"));
/* Gather information to apply concurrent changes. */
- chgdst.rel = NewHeap;
- chgdst.iistate = get_index_insert_state(NewHeap, ident_idx_new,
- &chgdst.ident_index);
- chgdst.ident_key = build_identity_key(ident_idx_new, OldHeap,
- &chgdst.ident_key_nentries);
- chgdst.file_seq = WORKER_FILE_SNAPSHOT + 1;
+ initialize_change_context(&chgcxt, NewHeap, ident_idx_new);
/*
* During testing, wait for another backend to perform concurrent data
@@ -3523,7 +3485,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* hold AccessExclusiveLock. (Quite some amount of WAL could have been
* written during the data copying and index creation.)
*/
- process_concurrent_changes(end_of_wal, &chgdst, false);
+ process_concurrent_changes(end_of_wal, &chgcxt, false);
/*
* Acquire AccessExclusiveLock on the table, its TOAST relation (if there
@@ -3608,7 +3570,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
* Apply the concurrent changes again. Indicate that the decoding worker
* won't be needed anymore.
*/
- process_concurrent_changes(end_of_wal, &chgdst, true);
+ process_concurrent_changes(end_of_wal, &chgcxt, true);
/* Remember info about rel before closing OldHeap */
relpersistence = OldHeap->rd_rel->relpersistence;
@@ -3659,8 +3621,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap,
table_close(NewHeap, NoLock);
/* Cleanup what we don't need anymore. (And close the identity index.) */
- pfree(chgdst.ident_key);
- free_index_insert_state(chgdst.iistate);
+ release_change_context(&chgcxt);
/*
* Swap the relations and their TOAST relations and TOAST indexes. This
@@ -3980,7 +3941,7 @@ repack_worker_internal(dsm_segment *seg)
for (;;)
{
- bool stop = decode_concurrent_changes(decoding_ctx, shared);
+ bool stop = decode_concurrent_changes(decoding_ctx, shared);
if (stop)
break;
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 72f7c0b992b..12bfd50d25b 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -426,7 +426,7 @@ CatCacheHeader
CatalogId
CatalogIdMapEntry
CatalogIndexState
-ChangeDest
+ChangeContext
ChangeVarNodes_callback
ChangeVarNodes_context
ChannelName
--
2.47.3
--gwom7bl7ogtszo4k
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
filename="v44-0006-rename-routines-on-the-logical-output-plugin-sid.patch"
^ permalink raw reply [nested|flat] 459+ messages in thread
end of thread, other threads:[~2026-03-23 20:27 UTC | newest]
Thread overview: 459+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2025-10-01 09:45 [PATCH v14 1/2] Key PGSTAT_KIND_RELATION by relfile locator Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
2026-03-23 20:27 [PATCH v44 05/10] Rename ChangeDest to ChangeContext, and absorb IndexInsertState Álvaro Herrera <alvherre@kurilemu.de>
This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox